package com.gyj.zookeeper.Hot.Util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateFunction {
    public static SimpleDateFormat fmt = new SimpleDateFormat();

    public static final long MILLIS_PER_MINUTE = 60 * 1000;
    public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
    public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;

    /**
     * 获取给定时间date的前day
     *
     * @param date
     * @param day
     * @return
     */
    public static Date getDateBeforeDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -day);
        Date _date = calendar.getTime();
        return _date;
    }

    /**
     * 获取给定时间后day
     *
     * @param date
     * @param day  Date
     *             each engineer has a duty to keep the code elegant
     */
    public static Date getDateAfterDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        Date _date = calendar.getTime();
        return _date;
    }

    /**
     * 获取给定时间后day
     *
     * @param date
     * @return Date
     * each engineer has a duty to keep the code elegant
     */
    public static Date getDateAfterHours(Date date, int hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hours);
        Date _date = calendar.getTime();
        return _date;
    }

    /**
     * 获取指定时间的日
     *
     * @param hour
     * @param minute
     * @param second
     * @return Date
     * each engineer has a duty to keep the code elegant
     */
    public static Date getDateSetTime(int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 获取指定格式时间字符
     *
     * @param pattern 自定义格 yyyy-MM-dd HH:mm:ss
     * @param date    Date类型日期
     * @return String 字符串格式日
     */
    public static String getStrDateFormat(String pattern, Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat();
        fmt.applyPattern(pattern);
        String dateStr = fmt.format(date);
        return dateStr;
    }

    /**
     * 对给定的时间值转为全文检索语法的时间 如将2006-05-02 12:12:12 转为全文索语法的时间 20060502121212
     */
    public static String getContentTimeNoSec(String strDate) {
        if (strDate == null || "null".equals(strDate)) {
            return ("null");
        }
        String strContent = strDate;
        strContent = strContent.replaceAll(" ", "");
        strContent = strContent.replaceAll("-", "");
        strContent = strContent.replaceAll(":", "");

        if (strContent.length() == 8) {
            strContent = strContent + "0000";
        } else if (strContent.length() == 12) {
            strContent = strContent + "00";
        }
        return (strContent);
    }

    /**
     * 以全文检索时间串形式返回给定时间若干小时前的时间
     */
    public static String getYMDBeforeHour(String curDate, int hours) {
        String oldtime = "";
        try {
            SimpleDateFormat fmt = new SimpleDateFormat();
            fmt.applyPattern("yyyy-MM-dd HH:mm:ss");
            Calendar caldate = new GregorianCalendar();
            Date date = fmt.parse(curDate);
            caldate.setTimeInMillis(date.getTime());

            caldate.add(Calendar.HOUR_OF_DAY, 0 - hours);
            fmt.applyPattern("yyyyMMddHHmmss");
            oldtime = fmt.format(caldate.getTime());
        } catch (Exception e) {

        }
        return oldtime;
    }

    /**
     * 以全文检索时间串形式返回给定时间若干秒之前的时间
     */
    public static String getYMDAfterSeconds(String curDate, int seconds) {
        String oldtime = "";
        try {
            SimpleDateFormat fmt = new SimpleDateFormat();
            fmt.applyPattern("yyyyMMddHHmmss");
            Calendar caldate = new GregorianCalendar();
            Date date = fmt.parse(curDate);
            caldate.setTimeInMillis(date.getTime());
            caldate.add(Calendar.SECOND, seconds);
            fmt.applyPattern("yyyyMMddHHmmss");
            oldtime = fmt.format(caldate.getTime());
        } catch (Exception e) {
        }
        return oldtime;
    }

    /**
     * 把多少天的数字转换成对应的日期格 比如今天2001-01-011天前就是2000-12-31
     */
    public static String getDBDateFromNum(String strNum) {
        String strDate = null;
        String strYear = null, strMonth = null, strDay = null;
        Calendar calendar = new GregorianCalendar();
        strDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(strDay) - Integer.parseInt(strNum));
        strYear = String.valueOf(calendar.get(Calendar.YEAR));
        strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        strDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        if (strMonth.length() == 1) {
            strMonth = "0" + strMonth;
        }
        if (strDay.length() == 1) {
            strDay = "0" + strDay;
        }
        strDate = strYear + "-" + strMonth + "-" + strDay;
        return strDate;
    }

    /**
     * 获取给定时间前N个小
     *
     * @param date
     * @return Date
     * each engineer has a duty to keep the code elegant
     */
    public static Date getDateBeforeHours(Date date, int hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, -hours);
        Date _date = calendar.getTime();
        return _date;
    }

    /**
     * 对给定的全文索时间 转为数据库的时 如将20060502121212 转为数据库的时间 2006-05-02 12:12:12
     *
     * @return String
     */
    public static String getDBTime(String strTime) {
        String strDBTime = "";
        if (strTime == null || "null".equals(strTime)) {
            return ("null");
        }

        try {
            SimpleDateFormat fm = new SimpleDateFormat("yyyyMMddHHmmss");
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (strTime.length() == 10) {
                fm = new SimpleDateFormat("yyyyMMddHH");
                fmt = new SimpleDateFormat("yyyy-MM-dd HH");
            } else if (strTime.length() == 8) {
                fm = new SimpleDateFormat("yyyyMMdd");
                fmt = new SimpleDateFormat("yyyy-MM-dd");
            }

            Date date = fm.parse(strTime);
            strDBTime = fmt.format(date);
        } catch (Exception e) {

        }
        return (strDBTime);
    }

    /**
     * 对给定的时间值转为全文检索语法的时间，不补时分秒 如将2006-05-02转为全文索语法的时间 20060502
     *
     * @param strDate String
     * @return String
     */
    public static String transContentTime(String strDate) {
        if (strDate == null || "null".equals(strDate)) {
            return ("null");
        }
        String strContent = strDate;
        strContent = strContent.replaceAll(" ", "");
        strContent = strContent.replaceAll("-", "");
        strContent = strContent.replaceAll(":", "");
        return (strContent);
    }

    /**
     * "yyyy-MM-dd HH:mm形式返回给定时间若干小时前的时间
     *
     * @param curDate String
     * @param hours   int
     * @return String
     */
    public static String getStrBeforeHour(String curDate, int hours) {
        String olddate = "";
        try {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

            if (curDate.length() == 19) {
                fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            } else if (curDate.length() == 16) {
                fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            } else if (curDate.length() == 13) {
                fmt = new SimpleDateFormat("yyyy-MM-dd HH");
            }
            Calendar caldate = new GregorianCalendar();
            Date date = fmt.parse(curDate);
            caldate.setTimeInMillis(date.getTime());

            caldate.add(Calendar.HOUR_OF_DAY, 0 - hours);

            olddate = fmt.format(caldate.getTime());
        } catch (Exception e) {

        }
        return olddate;
    }

    /**
     * 返回系统时间组成的字符串。例如：2005-08-28 15:00
     */
    public static String getSystemDay() {
        Calendar c = Calendar.getInstance();
        String returnValue = "";
        try {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            returnValue = fmt.format(c.getTime());
        } catch (Exception e) {

        }
        return returnValue;
    }

    /**
     * 24小时记时 将日(Date)转换为字符串形式,默认转换为："yyyy-MM-dd HH:mm:ss"
     *
     * @param date
     * @return
     */
    public static String formatDateSting(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        return strDate;
    }

    /**
     * 将输入的字符串日期转换为Date形式
     *
     * @param convertTime 日期格式为： "yyyy-MM-dd"
     * @return Date
     */
    public static Date getDate(String convertTime) {
        Date returnValue = null;
        try {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            returnValue = fmt.parse(convertTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnValue;
    }

    /**
     * 获取指定日期000
     */
    public static Date getZeroHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取今天的零点零
     */
    public static String getTodayBeginZero() {
        Date date = new Date();
        date = getZeroHour(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String todaydate = sdf.format(date);
        return todaydate;
    }

    /**
     * 获取当前时间几小时之前的日期时间
     * @param hour
     */
    public static Date getHoursAgoDate(int hour) {
        Calendar twoHoursAgoDate = Calendar.getInstance();
        twoHoursAgoDate.set(Calendar.MILLISECOND, 0);
        twoHoursAgoDate.set(Calendar.SECOND, 0);
        twoHoursAgoDate.set(Calendar.MINUTE, 0);
        twoHoursAgoDate.add(Calendar.HOUR_OF_DAY, -hour);
        return twoHoursAgoDate.getTime();
    }

    /**
     * 获取传Date整点时间
     */
    public static String getCurHTime(Date date) {
        String returnValue = "";
        try {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHH0000");
            returnValue = fmt.format(date.getTime());
        } catch (Exception e) {

        }
        return returnValue;
    }


    public static Date getBeforeMinute(Date date, int iMinutes) {
        try {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.MINUTE, 0 - iMinutes);
            return calendar.getTime();
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getAfterMinute(Date date, int iMinutes, int second) {
        try {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.MINUTE, iMinutes);
            calendar.add(Calendar.SECOND, second);
            return calendar.getTime();
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getDateBySet(Integer year, Integer month, Integer dayOfMonth,
                                    Integer hourOfDay, Integer minute, Integer second, Integer millisecond) {
        Calendar cal = Calendar.getInstance();

        if (year != null) {
            cal.set(Calendar.YEAR, year);
        }
        if (month != null) {
            cal.set(Calendar.MONTH, month);
        }
        if (dayOfMonth != null) {
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        }
        if (hourOfDay != null) {
            cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
        }
        if (minute != null) {
            cal.set(Calendar.MINUTE, minute);
        }
        if (second != null) {
            cal.set(Calendar.SECOND, second);
        }
        if (millisecond != null) {
            cal.set(Calendar.MILLISECOND, millisecond);
        }

        return cal.getTime();
    }


    public static void main(String[] args) {
        System.out.println(getCurHTime(new Date()));
        System.out.println(getStrDateFormat("yyyyMMddHH0000", new Date()));
        Date endDate = new Date();
        Date beginDate = DateFunction.getDateBeforeHours(endDate, 1);
        System.out.println("beginDate:" + DateFunction.getStrDateFormat("yyyyMMddHH0000", endDate));
        System.out.println("endDate:" + DateFunction.getStrDateFormat("yyyyMMddHH0000", beginDate));
    }
}
